import 'dart:convert' show json;

class WorldCityInfo {

  Location location;
  WorldCityInfo.empty();
  WorldCityInfo.fromParams({this.location});

  factory WorldCityInfo(jsonStr) => jsonStr == null ? null : jsonStr is String ? new WorldCityInfo.fromJson(json.decode(jsonStr)) : new WorldCityInfo.fromJson(jsonStr);

  WorldCityInfo.fromJson(jsonRes) {
    location = jsonRes['location'] == null ? null : new Location.fromJson(jsonRes['location']);
  }

  @override
  String toString() {
    return '{"location": $location}';
  }
}

class Location {

  List<CountryRegion> countryRegion;

  Location.fromParams({this.countryRegion});

  Location.fromJson(jsonRes) {
    countryRegion = jsonRes['countryRegion'] == null ? null : [];

    for (var countryRegionItem in countryRegion == null ? [] : jsonRes['countryRegion']){
      countryRegion.add(countryRegionItem == null ? null : new CountryRegion.fromJson(countryRegionItem));
    }
  }

  @override
  String toString() {
    return '{"countryRegion": $countryRegion}';
  }
}

class CountryRegion {

  String code;
  String name;
  List state = [];

  CountryRegion.fromParams({this.code, this.name, this.state});

  CountryRegion.fromJson(jsonRes) {
    code = jsonRes['code'];
    name = jsonRes['name'];
    state = jsonRes['state'];//== null ? null : [];
    if(jsonRes['state'] is List){
      for(int i = 0;i < jsonRes['state'].length;i++){
        state.add((State.fromJson(jsonRes['state'])));
      }
    }else{
      state.add(State.fromJson(jsonRes['state']));
    }
  }

  @override
  String toString() {
    return '{"code": ${code != null?'${json.encode(code)}':'null'},"name": ${name != null?'${json.encode(name)}':'null'},"state": $state}';
  }
}

class State {

  String code = 'code';
  String name = 'name';
  List city = [];

  State.fromParams({this.code, this.name, this.city});

  State.fromJson(jsonRes) {
//    code = jsonRes['Code'];
//    name = jsonRes['Name'];
    if(jsonRes is Map){///Map
      city = jsonRes['city'] == null ? null : [];
    }else if(jsonRes is List) {///list
      var v = jsonRes.map((e) => City.fromJson(e)).toList();
      city.addAll(v);
    }else {///String
      print('==============>$jsonRes');
    }
  }

  @override
  String toString() {
    return '{"code": ${code != null?'${json.encode(code)}':'null'},"name": ${name != null?'${json.encode(name)}':'null'},"city": $city}';
  }
}

class City {
  City.empty();
  String code = '没有字段';
  String name = '没有字段';

  City.fromParams({this.code, this.name});

  City.fromJson(jsonRes) {
    print('++++++++++>$jsonRes' + jsonRes.runtimeType.toString());
    if(jsonRes is Map){
      code = jsonRes['code'];
      name = jsonRes['name'];
    }else{

    }
  }

  @override
  String toString() {
    return '{"code": ${code != null?'${json.encode(code)}':'null'},"name": ${name != null?'${json.encode(name)}':'null'}}';
  }
}

